# -*- coding: utf-8 -*-
"""
@author: wyb
@date: 2023/12/28
@time: 21:41
@desc: Description
"""

from bs4 import BeautifulSoup
import re

html = """
<html>
    <head>
        <title>The Dormouse's story</title>
    </head>
    <body>
        <p class="story">
            Once upon a time there were three little sisters; and their names were
            <a href="http://example.com/elsie" class="sister" id="link1">
                <span>Elsie</span>
            </a>
            <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> 
            and
            <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
            and they lived at the bottom of a well.
        </p>
        <p class="story">...</p>
"""
soup = BeautifulSoup(html, 'lxml')
# print(soup.p.contents)
# # region contents
#
# for i, content in enumerate(soup.p.contents):
#     if isinstance(content, str):
#         print(i, content.strip())
#     else:
#         print(i, content)
# # endregion
# # region child
# for i, child in enumerate(soup.p.children):
#     if isinstance(child, str):
#         print(i, child.strip())
#     else:
#         print(i, child)
# # endregion
# # region descendants
# for i, child in enumerate(soup.p.descendants):
#     if isinstance(child, str):
#         print(i, child.strip())
#     else:
#         print(i, child)
# # endregion

# region parent
# print(soup.a.parent)
# endregion

# region parents
# print(type(soup.a.parents))
# print(list(enumerate(soup.a.parents)))
# endregion

# region slibing
# print('next slibing:')
# print(type(soup.a.next_sibling))
# print(soup.a.id)
# print(soup.a.next_sibling)
# print(soup.a.next_sibling.string)
# print('parent:')
# print(type(soup.a.parents))
# print(list(soup.a.parents)[0])
# print(list(soup.a.parents)[0].attrs['class'])
# endregion
# region find
# a_tag = soup.find("a")
# print(type(a_tag))
# print(a_tag.span.text)
# next_slibling = a_tag.find_next_sibling()
# next_slibling2 = a_tag.next_sibling
# print(type(next_slibling2))
# print(next_slibling2.string)
# print(next_slibling.text)
# print(next_slibling.attrs['href'])
# endregion

# # region findall
# html2 = '''
# <div class="panel">
#     <div class="panel-heading">
#         <h4>Hello</h4>
#     </div>
#     <div class="panel-body">
#         <ul class="list" id="list-1">
#             <li class="element">Foo</li>
#             <li class="element">Bar</li>
#             <li class="element">Jay</li>
#         </ul>
#         <ul class="list list-small" id="list-2">
#             <li class="element">Foo</li>
#             <li class="element">Bar</li>
#         </ul>
#     </div>
# </div>
# '''
# soup2 = BeautifulSoup(html2, 'lxml')
# ullist = soup2.find_all(name='ul')
# print(type(soup2.find_all(name='ul')[0]))
# for ul in ullist:
#     lilist = ul.find_all(name="li")
#     print(lilist)
#     for li in lilist:
#         print(li.string)
#
# # endregion

# # region attrs
# html3 = '''
# <div class="panel">
#     <div class="panel-heading">
#         <h4>Hello</h4>
#     </div>
#     <div class="panel-body">
#         <ul class="list" id="list-1" name="elements">
#             <li class="element">Foo</li>
#             <li class="element">Bar</li>
#             <li class="element">Jay</li>
#         </ul>
#         <ul class="list list-small" id="list-2">
#             <li class="element">Foo</li>
#             <li class="element">Bar</li>
#         </ul>
#     </div>
# </div>
# '''
# soup3 = BeautifulSoup(html3,'lxml')
# print(soup3.find_all(attrs={'id':'list-1'}))
# print(soup3.find_all(attrs={'name':'elements'}))
# print(soup3.find_all(class_ = 'element'))
# # endregion

# # region text
# html4 = '''
# <div class="panel">
#     <div class="panel-body">
#         <a>Hello, this is a link</a>
#         <a>Hello, this is a link, too</a>
#     </div>
# </div>
# '''
# soup4 = BeautifulSoup(html4, 'lxml')
# print(soup4.find_all(string=re.compile('link')))
#
# # endregion

# #region find
# html5='''
# <div class="panel">
#     <div class="panel-heading">
#         <h4>Hello</h4>
#     </div>
#     <div class="panel-body">
#         <ul class="list" id="list-1">
#             <li class="element">Foo</li>
#             <li class="element">Bar</li>
#             <li class="element">Jay</li>
#         </ul>
#         <ul class="list list-small" id="list-2">
#             <li class="element">Foo</li>
#             <li class="element">Bar</li>
#         </ul>
#     </div>
# </div>
# '''
#
# soup5 = BeautifulSoup(html5,'lxml')
# ul = soup5.find(name = 'ul')
# print(type(ul))
# print(ul)
#
# ul2 = soup5.find(class_ = 'element')
# print(type(ul2))
# print(ul2)
# #endregion
# region css selector

html6 = '''
<div class="panel">
    <div class="panel-heading">
        <h4>Hello</h4>
    </div>
    <div class="panel-body">
        <ul class="list" id="list-1">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
            <li class="element">Jay</li>
        </ul>
        <ul class="list list-small" id="list-2">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
        </ul>
    </div>
</div>
'''
soup6 = BeautifulSoup(html6, 'lxml')
print(soup6.select('.panel .panel-heading'))
print(soup6.select('ul li'))
print(soup6.select('#list-2 .element'))
print(type(soup6.select('ul')[0]))

ullist = soup6.select('ul')
for ul in ullist:
    lilist = ul.select('li')
    print(lilist)
    for li in lilist:
        print(li.string)
        print(li.attrs['class'])

# endregion
